from flask import Flask, render_template, request
from datetime import datetime
from markupsafe import Markup

if __name__ == "__main__":
    app = Flask(__name__)
    # 在生产环境启用缓存
    # app.jinja_env.cache = True

    # 定义全局变量
    @app.context_processor
    def inject_user():
        return dict(current_year=datetime.now().year)

    @app.template_filter("format_date")
    def format_date(value):
        return value.strftime("%y-%m-%d")

    @app.template_filter("double")
    def double(value):
        return value + value

    @app.template_filter("prefix")
    def prefix(value, pre):
        return pre + value

    @app.template_filter("suffix")
    def suffix(value, suf):
        return value + suf

    @app.route("/")
    def basic():
        return render_template(
            "basic.html",
            user={"name": "张三", "is_editor": False},
            price=123.456,
            items=[{"name": "A"}, {"name": "B"}, {"name": "C"}],
        )

    @app.route("/user")
    def user():
        return render_template("user.html")

    @app.route("/filter")
    def filter():
        return render_template(
            "filter.html",
            text="HELLO",
            html_content="<h1>一级标题</h1>",
            html_content2=Markup("<h1>一级标题</h1>"),
            list=["A", "B", "C"],
            name="zhangsan",
            current_date=datetime.now().date(),
            word="word",
        )

    @app.route("/macro")
    def macro():
        return render_template("macro.html")

    @app.route("/admin/user")
    def admin_user():
        return render_template("admin/user.html")

    app.run(debug=True)


# sass less stylus tailwindcss
# 1. sass=> css 引入的模板
